package br.edu.ufcg.fpc;

import java.util.concurrent.locks.ReentrantLock;

/**
 * Protege a regiao critica utilizando a trava ReentrantLock.
 */
public class ReentrantLockCR extends CriticalRegion {

	static ReentrantLock LOCK;
	
	public ReentrantLockCR(Boolean fair) {
		if (LOCK == null)
			LOCK = new ReentrantLock(fair);
	}
	
	@Override
	protected void executeCriticalRegion() {
		long before = System.nanoTime();
		LOCK.lock();
		addTime(System.nanoTime() - before);
		increment();
		LOCK.unlock();
	}
	
}
